package com.wibo.account;


import weibo4android.Status;

import com.wibo.common.Utility;
import com.wibo.common.WeiboItem;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.text.Html;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WeiboListItemView extends RelativeLayout {
	private Context mContext;
	
	private static final String TAG = "WeiboListItemView";
	
	private RelativeLayout mOriginalWeiboLayout;
	private ImageView mWeiboUserPic;
	private ImageView mWeiboPic;
	private ImageView mPicLogo;
	private ImageView mCommentLogo;
	private ImageView mTransmitLogo;
	private ImageView mVLogo;
	private ImageView mGpsLogo;
	private ImageView mOriginalWeiboPic;
	private TextView mUserName;
	private TextView mWeiboText;
	private TextView mOriginalText;
	private TextView mSentTime;
	private TextView mClientName;
	private TextView mCommentCount;
	private TextView mTransmitCount;
	
	private EmotionsParser mParser;
	
	private CUserManager mUserManager = null;

	public WeiboListItemView(Context context) {
		super(context);
		mUserManager = CUserManager.getInstance(context);
		initialize(context);
	}
	public WeiboListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mUserManager = CUserManager.getInstance(context);
        initialize(context);
    }
	
	private int getListItemSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			return R.layout.weibolistitem;
		case 1:
			return R.layout.weibolistitem_2;
		default:
			return R.layout.weibolistitem;
		}
	}
	
	private float mUserNameSize;
	private float mWeiboTextSize;
	private float mOriginalTextSize;
	private float mSentTimeSize;
	private float mClientNameSize;
	private float mCommentCountSize;
	private float mTransmitCountSize;
	
	private void setTextSize() {
		int add = 0;
		switch (mUserManager.getTextSize()) {
		case CUserManager.SMALL:
			add = 0;
			mUserName.setTextSize(mUserNameSize + add);
			break;
		case CUserManager.MID:
			add = 1;
			mUserName.setTextSize(mUserNameSize + add);
			break;
		case CUserManager.LARGE:
			mUserName.setTextSize(mUserNameSize + CUserManager.MID);
			add = 2;
			break;
		default:
			break;
		}
		
		
		mWeiboText.setTextSize(mWeiboTextSize + add);
		mOriginalText.setTextSize(mOriginalTextSize + add);
		//mSentTime.setTextSize(mSentTimeSize + add);
		//mClientName.setTextSize(mClientNameSize + add);
		//mCommentCount.setTextSize(mCommentCountSize + add);
		//mTransmitCount.setTextSize(mTransmitCountSize + add);
	}
	
	private void initialize(Context context) {
    	this.mContext = context;
    	mParser = new EmotionsParser(mContext); 
    	View view = LayoutInflater.from(this.mContext).inflate(getListItemSkin(), null);
    	
    	mOriginalWeiboLayout = (RelativeLayout) view.findViewById(R.id.originalRelativeLayout);
    	
    	mWeiboUserPic = (ImageView)view.findViewById(R.id.userPic);
    	mWeiboUserPic.setVisibility(ImageView.GONE);
    	mWeiboPic = (ImageView)view.findViewById(R.id.weiboPic);
    	mWeiboPic.setVisibility(ImageView.GONE);
    	mPicLogo = (ImageView)view.findViewById(R.id.picLogo);
    	mPicLogo.setVisibility(ImageView.GONE);
    	mCommentLogo = (ImageView)view.findViewById(R.id.commentLogo);
    	mCommentLogo.setVisibility(ImageView.GONE);
    	mTransmitLogo = (ImageView)view.findViewById(R.id.transmitLogo);
    	mTransmitLogo.setVisibility(ImageView.GONE);
    	
    	mOriginalWeiboPic = (ImageView)view.findViewById(R.id.originalWeiboPic);
    	mOriginalWeiboPic.setVisibility(ImageView.GONE);

    	mUserName = (TextView)view.findViewById(R.id.userName);
    	TextPaint tp = mUserName.getPaint(); 
    	tp.setFakeBoldText(true);
    	mWeiboText = (TextView)view.findViewById(R.id.weiboText);
    	mVLogo = (ImageView)view.findViewById(R.id.vLogo);
    	mVLogo.setVisibility(ImageView.GONE);
    	mGpsLogo = (ImageView) view.findViewById(R.id.gps_logo);
    	mGpsLogo.setVisibility(ImageView.GONE);
    	mOriginalText = (TextView)view.findViewById(R.id.originalWeiboText);
    	mOriginalText.setVisibility(TextView.GONE);
    	mSentTime = (TextView)view.findViewById(R.id.sentTime);
    	mClientName = (TextView)view.findViewById(R.id.clientNameText);
    	mCommentCount = (TextView)view.findViewById(R.id.commentCountText);
    	mCommentCount.setVisibility(TextView.GONE);
    	mTransmitCount = (TextView)view.findViewById(R.id.transmitCountText);
    	mTransmitCount.setVisibility(TextView.GONE);
    	
    	mUserNameSize = mUserName.getTextSize();
    	mWeiboTextSize = mWeiboText.getTextSize();
    	mOriginalTextSize = mOriginalText.getTextSize();
    	mSentTimeSize = mSentTime.getTextSize();
    	mClientNameSize = mClientName.getTextSize();
    	mCommentCountSize = mCommentCount.getTextSize();
    	mTransmitCountSize = mTransmitCount.getTextSize();
    	addView(view);
    	// setBackgroundResource(R.drawable.myitemlayout);    	
	}
	
	public void updateView(final WeiboItem weiboItem, String position) {
		
		setTextSize();
		if (weiboItem.getmWeiboPic() != null && !weiboItem.getmWeiboPic().isEmpty()) {
			mWeiboPic.setImageBitmap(Utility.getBitmap(mContext, weiboItem.getmWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC));
			mWeiboPic.setVisibility(ImageView.VISIBLE);
//			if (!Utility.isImageExist(mContext, weiboItem.getmWeiboPic(), Utility.WEIBO_PIC, WiboMainActivity.THUMBNAIL_PIC)){
//				mWeiboPic.setImageDrawable(mContext.getResources().getDrawable(R.drawable.weibo_pic));
//				mWeiboPic.setVisibility(ImageView.VISIBLE);
//			}else{
//				if ((bitmap = BitmapFactory.decodeFile(Utility.getImagePath(mContext, weiboItem.getmWeiboPic(), Utility.WEIBO_PIC, WiboMainActivity.THUMBNAIL_PIC))) != null){
//					mWeiboPic.setImageBitmap(bitmap);
//					mWeiboPic.setVisibility(ImageView.VISIBLE);
//				}
//			}
			
		}else{
			mWeiboPic.setVisibility(ImageView.GONE);
			mWeiboPic.setImageBitmap(null);
		}
		
		if (weiboItem.getmWeiboUserPic() != null) {
			mWeiboUserPic.setImageBitmap(Utility.getBitmap(mContext, weiboItem.getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC));
			mWeiboUserPic.setVisibility(ImageView.VISIBLE);
//			if (!Utility.isImageExist(mContext, weiboItem.getmWeiboUserPic(), Utility.WEIBO_PIC, WiboMainActivity.USER_PIC)){
//				mWeiboUserPic.setImageDrawable(mContext.getResources().getDrawable(R.drawable.user_pic));
//				mWeiboUserPic.setVisibility(ImageView.VISIBLE);
//			}else {
//				if ((bitmap = BitmapFactory.decodeFile(Utility.getImagePath(mContext, weiboItem.getmWeiboUserPic(), Utility.WEIBO_PIC, WiboMainActivity.USER_PIC))) != null){
//					mWeiboUserPic.setImageBitmap(bitmap);
//					mWeiboUserPic.setVisibility(ImageView.VISIBLE);
//				}
//			}
			mWeiboUserPic.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, WiboUserDetailsActivity.class);
        			Bundle bundle = new Bundle();
            		bundle.putLong("userId", weiboItem.getmUserId());
            		intent.putExtras(bundle);
            		mContext.startActivity(intent);
				}
			});
		}else{
			mWeiboUserPic.setVisibility(ImageView.GONE);
			mWeiboUserPic.setImageBitmap(null);
		}
		
		if (weiboItem.ismVLogo()) {
			mVLogo.setVisibility(ImageView.VISIBLE);
		}else{
			mVLogo.setVisibility(ImageView.GONE);
		}
		
		if (weiboItem.getmLatitude() != null && weiboItem.getmLongitude() != null 
				&& !weiboItem.getmLatitude().equals("-1.0") && !weiboItem.getmLongitude().equals("-1.0")) {
			mGpsLogo.setVisibility(ImageView.VISIBLE);
		}else{
			mGpsLogo.setVisibility(ImageView.GONE);
		}

		if ((weiboItem.getmWeiboPic() != null && !weiboItem.getmWeiboPic().isEmpty())
				|| (weiboItem.getmRtrootWeiboPic() != null && !weiboItem.getmRtrootWeiboPic().isEmpty())){
			mPicLogo.setVisibility(ImageView.VISIBLE);
		}else{
			mPicLogo.setVisibility(ImageView.GONE);
		}
//
//		if (weiboItem.getmCommentLogo() != null){
//			if ((bitmap = BitmapFactory.decodeFile(weiboItem.getmCommentLogo())) != null){
//				mCommentLogo.setImageBitmap(bitmap);
//				mCommentLogo.setVisibility(ImageView.VISIBLE);
//			}
//		}else{
//			mCommentLogo.setVisibility(ImageView.GONE);
//			mPicLogo.setImageBitmap(null);
//		}
//
//		if (weiboItem.getmTransmitLogo() != null){
//			if ((bitmap = BitmapFactory.decodeFile(weiboItem.getmTransmitLogo())) != null){
//				mTransmitLogo.setImageBitmap(bitmap);
//				mTransmitLogo.setVisibility(ImageView.VISIBLE);
//			}
//		}else{
//			mCommentLogo.setVisibility(ImageView.GONE);
//			mPicLogo.setImageBitmap(null);
//		}
//
//		if (weiboItem.getmVLogo() != null){
//			if ((bitmap = BitmapFactory.decodeFile(weiboItem.getmVLogo())) != null){
//				mVLogo.setImageBitmap(bitmap);
//				mVLogo.setVisibility(ImageView.VISIBLE);
//			}
//		}else{
//			mCommentLogo.setVisibility(ImageView.GONE);
//			mPicLogo.setImageBitmap(null);
//		}

		
		
		if (weiboItem.getmUserName() != null)
			mUserName.setText(mParser.replace(weiboItem.getmUserName(), 18, 18));
		if (weiboItem.getmWeiboText() != null)
			mWeiboText.setText(mParser.replace(weiboItem.getmWeiboText(), 18, 18));
		
		if (weiboItem.getmRtrootWeiboId() > 0) {
			if (weiboItem.getmRtrootText() != null && !weiboItem.getmRtrootText().isEmpty()){
				mOriginalWeiboLayout.setVisibility(RelativeLayout.VISIBLE);
				mOriginalText.setText(mParser.highlight(mParser.replace(weiboItem.getmRtrootUsername() + ": " + weiboItem.getmRtrootText(), 18, 18),
						0, weiboItem.getmRtrootUsername().length(), mContext.getResources().getColor(R.color.sky_blue)/*Color.BLUE*/));
				mOriginalText.setVisibility(TextView.VISIBLE);
				mWeiboPic.setVisibility(ImageView.GONE);
				
				if (weiboItem.getmRtrootWeiboPic() != null && !weiboItem.getmRtrootWeiboPic().isEmpty()){
//					if (!Utility.isImageExist(mContext, weiboItem.getmRtrootWeiboPic(), Utility.WEIBO_PIC, WiboMainActivity.USER_PIC)){
//						mOriginalWeiboPic.setImageDrawable(mContext.getResources().getDrawable(R.drawable.weibo_pic));
//						mOriginalWeiboPic.setVisibility(ImageView.VISIBLE);
//					}else{
//						if ((bitmap = BitmapFactory.decodeFile(Utility.getImagePath(mContext, weiboItem.getmRtrootWeiboPic(), 
//								Utility.WEIBO_PIC, WiboMainActivity.THUMBNAIL_PIC))) != null){
//							mOriginalWeiboPic.setImageBitmap(bitmap);
//							mOriginalWeiboPic.setVisibility(ImageView.VISIBLE);
//						}else{
//							mOriginalWeiboPic.setVisibility(ImageView.GONE);
//							mOriginalWeiboPic.setImageBitmap(null);
//						}
//					}
					mOriginalWeiboPic.setImageBitmap(Utility.getBitmap(mContext, weiboItem.getmRtrootWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC));
					mOriginalWeiboPic.setVisibility(ImageView.VISIBLE);
				}else{
					mOriginalWeiboPic.setVisibility(ImageView.GONE);
					mOriginalWeiboPic.setImageBitmap(null);
				}
			}else{
				mOriginalWeiboLayout.setVisibility(RelativeLayout.GONE);
				mOriginalText.setVisibility(TextView.GONE);
				mOriginalWeiboPic.setVisibility(ImageView.GONE);
				mOriginalWeiboPic.setImageBitmap(null);
			}
		}else{
			mOriginalWeiboLayout.setVisibility(RelativeLayout.GONE);
			mOriginalText.setVisibility(TextView.GONE);
			mOriginalWeiboPic.setVisibility(ImageView.GONE);
			mOriginalWeiboPic.setImageBitmap(null);
		}
		
		
		
		if (weiboItem.getmSentTime() != null)
			mSentTime.setText(weiboItem.getmSentTime());
		
		if (weiboItem.getmClientName() != null)
			mClientName.setText(Html.fromHtml(weiboItem.getmClientName()));
		
		if (weiboItem.getmCommentCount() != null && !weiboItem.getmCommentCount().equalsIgnoreCase("0")){
			mCommentCount.setText(weiboItem.getmCommentCount());
			mCommentCount.setVisibility(TextView.VISIBLE);
			mCommentLogo.setVisibility(ImageView.VISIBLE);			
		}else {
			mCommentCount.setVisibility(TextView.INVISIBLE);
			mCommentLogo.setVisibility(ImageView.INVISIBLE);	
		}
		
		if (weiboItem.getmTransmitCount() != null && !weiboItem.getmTransmitCount().equalsIgnoreCase("0")){
			mTransmitCount.setText(weiboItem.getmTransmitCount());
			mTransmitCount.setVisibility(TextView.VISIBLE);
			mTransmitLogo.setVisibility(ImageView.VISIBLE);	
		}else{
			mTransmitCount.setVisibility(TextView.INVISIBLE);
			mTransmitLogo.setVisibility(ImageView.INVISIBLE);	
		}
		
		this.setTag(position);
	}
	

}
